package boardgamer.yavalath.client;

import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import boardgamer.common.client.Display;
import boardgamer.common.client.layout.BitmapLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.TextLayout;
import boardgamer.yavalath.client.resources.ImageHandler;

/**
 * Displays a letter with a hexagon behind it
 *
 * @author garysoed
 */
public class LetterHex implements Display {

  private final TextLayout text;
  private final BitmapLayout hex;
  private final LayoutGroup group;

  public LetterHex(char letter, int hexColor, PointF offset, Paint textPaint, boolean isVertical) {
    this.text = new TextLayout(String.valueOf(letter), textPaint);

    Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    bitmapPaint.setColorFilter(new LightingColorFilter(hexColor, 0));
    this.hex = new BitmapLayout(
        isVertical ? ImageHandler.tileVertical : ImageHandler.tile,
        bitmapPaint);

    this.group = new LayoutGroup();
    this.group
        .add(hex).atCurrentRect()
        .add(text).atCurrentRect()
        .horizontalAlign(LayoutGroup.HorizontalAlign.CENTER)
        .verticalAlign(LayoutGroup.VerticalAlign.CENTER)
        .offset(offset.x, offset.y);
  }

  @Override
  public void draw(Canvas canvas) {
    hex.draw(canvas);
    text.draw(canvas);
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    this.group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return new RectF(group.getBoundingRect());
  }
}
